package genericdemo;

//代表将来指定的参数类型一定是实现了这个接口的
//泛型的上界

class Alg1<E extends Comparable<E>> {
    //找出数组最大值
    public E findMax(E[] array) {
        E max = array[0];
        for(int i = 1; i < array.length;i++) {
            if ((max.compareTo(array[i])) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2<E extends Number> {

}

class Alg3 {
    //找出数组最大值
    public<E extends Comparable<E>> E findMax(E[] array) {
        E max = array[0];
        for(int i = 1; i < array.length;i++) {
            if ((max.compareTo(array[i])) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg4 {
    //static 不用实例化对象
    public static <E extends Comparable<E>> E findMax(E[] array) {
        E max = array[0];
        for(int i = 1; i < array.length;i++) {
            if ((max.compareTo(array[i])) < 0) {
                max = array[i];
            }
        }
        return max;
    }

}
public class Test2 {
    public static void main(String[] args) {
        Integer[] array = {1,3,5,2,5,62,12};
        Integer val = Alg4.<Integer>findMax(array);
        System.out.println(val);
    }
    public static void main3(String[] args) {
        Alg3 alg3 = new Alg3();
        Integer[] array = {1,3,5,2,5,62,12};
        Integer val = alg3.<Integer>findMax(array);
        System.out.println(val);
    }
    public static void main2(String[] args) {
        Alg2<Integer> alg1 = new Alg2<>();
        Alg2<Number> alg2 = new Alg2<>();
        Alg2<Double> alg3 = new Alg2<>();
    }
    public static void main1(String[] args) {
        Alg1<Integer> alg = new Alg1<>();
        Integer[] array = {1,3,5,2,5,62,12};
        Integer val = alg.findMax(array);
        System.out.println(val);
    }
}
